package yur.wedding.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import yur.wedding.common.annotation.SysLog;
import yur.wedding.common.utils.Constant;
import yur.wedding.exception.RRException;
import yur.wedding.modules.sys.entity.SysDoorEntity;
import yur.wedding.modules.sys.entity.SysMenuEntity;
import yur.wedding.modules.sys.service.SysDoorService;


import yur.wedding.utils.R;
import yur.wedding.validator.ValidatorUtils;


/**
 * 门户管理
 *
 * @author Yur
 * @email ${email}
 * @date 2022-05-02 22:54:49
 */
@RestController
@RequestMapping("sys/sysdoor")
public class SysDoorController extends AbstractController {
    @Autowired
    private SysDoorService sysDoorService;


    /**
     * 前端菜单
     */
    @RequestMapping("/nav/{Id}")
    public R nav(@PathVariable("Id") Long Id) {


        List<SysDoorEntity> menulist = sysDoorService.getMenuList(Id, getUserId());
        return R.ok().put("menulist", menulist);
    }

    /**
     * 新增文档时 目录权限
     */
    @RequestMapping("/docuMentDirecTory")
    public R docuMentDirecTory() {


        List<SysDoorEntity> menulist = sysDoorService.docuMentDirecTory();
        return R.ok().put("menulist", menulist);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysdoor:list")
    public List list(@RequestParam Map<String, Object> params) {
        //查询门户列表
        List<SysDoorEntity> doorList = sysDoorService.list();

        //查询门户目录的 父级名称
        for (SysDoorEntity sysDeptEntity : doorList) {
            SysDoorEntity parentName = sysDoorService.getById(sysDeptEntity.getParentId());
            if (parentName != null) {
                sysDeptEntity.setParentName(parentName.getName());
            }

        }
        return doorList;
    }

    @RequestMapping("/select")
    @RequiresPermissions("sys:sysdoor:select")
    public R select() {
        //查询菜单  排除按钮
        List<SysDoorEntity> doorList = sysDoorService.queryNotButtonList();

        SysDoorEntity root = new SysDoorEntity();
        //设置顶级菜单
        root.setDoorId(0L);
        root.setName("");
        root.setParentId(-1L);
        root.setOpen(true);
        doorList.add(root);


        return R.ok().put("doorList", doorList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{doorId}")
    @RequiresPermissions("sys:sysdoor:info")
    public R info(@PathVariable("doorId") Long doorId) {

        SysDoorEntity sysDoor = sysDoorService.getById(doorId);

        return R.ok().put("door", sysDoor);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysdoor:save")
    public R save(@RequestBody SysDoorEntity sysDoor) {

        try {
            verifyForm(sysDoor);
        } catch (Exception e) {
            return R.error(500, e.toString());
        }
        sysDoorService.save(sysDoor);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysdoor:update")
    public R update(@RequestBody SysDoorEntity sysDoor) {
        verifyForm(sysDoor);
        sysDoorService.updateById(sysDoor);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除门户菜单")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysdoor:delete")
    public R delete(Long doorIds) {
        //系统门户菜单不能删除
        if (doorIds == 41) {
            return R.error("该门户为主门户不能删除");
        }
        //存在菜单不能删除
        List<SysDoorEntity> entities = sysDoorService.queryListParentId(doorIds);
        if (entities.size() > 0) {
            return R.error("存在子门户不能删除");
        }
        sysDoorService.removeByIds(Arrays.asList(doorIds));

        return R.ok();
    }


    /**
     * 校验参数
     */
    public void verifyForm(SysDoorEntity doorEntity) {

        //1 .菜单名称不能为空
        if (StringUtils.isEmpty(doorEntity.getName())) {
            throw new RRException("菜单名称不能为空");
        }
        //2 .上级菜单不能为空
        if (doorEntity.getParentId() == null) {
            throw new RRException("上级菜单不能为空");
        }
        //3 .菜单url不能为空
        if (doorEntity.getType() == Constant.MenuType.MENU.getValue()) {
            if (doorEntity.getUrl() == null) {
                throw new RRException("菜单url不能为空");
            }
        }
        //获取上级的类型  以便下面的校验
        int parentId = Constant.MenuType.CATALOG.getValue();
        if (doorEntity.getParentId() != 0) {
            SysDoorEntity sysDoor = sysDoorService.getById(doorEntity.getParentId());
            parentId = sysDoor.getType();
        }
        //4 .菜单或目录的上级只能是目录类型
        if (doorEntity.getType() == Constant.MenuType.CATALOG.getValue() || doorEntity.getType() == Constant.MenuType.MENU.getValue()) {
            if (parentId != Constant.MenuType.CATALOG.getValue()) {
                throw new RRException("主菜单或目录的上级只能是目录类型");
            }
            return;

        }
        //5 .按钮的上级只能是菜单类型
        if (doorEntity.getType() == Constant.MenuType.BUTTON.getValue()) {
            if (parentId != Constant.MenuType.MENU.getValue()) {
                throw new RRException("按钮的上级只能是菜单类型");

            }
            return;
        }

    }
}
